home *** CD-ROM | disk | FTP | other *** search
- Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
- From: grantp@usa.pipeline.com(Pete Grant)
- Newsgroups: comp.lang.c++
- Subject: Re: C++ questions - please help
- Date: 21 Mar 1996 17:04:43 GMT
- Organization: Kalevi, Inc.
- Message-ID: <4is27b$cs5@news1.h1.usa.pipeline.com>
- References: <4irp34$b6q@GRAPEVINE.LCS.MIT.EDU>
- NNTP-Posting-Host: 38.8.53.2
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete Grant)
- X-Newsreader: Pipeline v3.5.0
-
- On Mar 21, 1996 14:28:52 in article <C++ questions - please help>,
- 'frankkim@antoniades.lcs.mit.edu (Frank Kim)' wrote:
-
-
- >
- >Hi,
- >
- >I was looking through some code and some of the stuff just stumped me.
- >
- >Question #1:
- >
- >There is a class defined like this...
- >
- >class faxRmApp : public FaxClient {
- >private:
- >fxStr appName; // for error messages
- >fxStrArray jobids;
- >fxStr request;
- >
- >void usage();
- >void printError(const char* fmt, ...);
- >void printWarning(const char* fmt, ...);
- >public:
- >faxRmApp();
- >~faxRmApp();
- >
- >void initialize(int argc, char** argv);
- >void open();
- >
- >void recvConf(const char* cmd, const char* tag);
- >void recvEof();
- >void recvError(const int err);
- >};
- >
- >and its constructor looks like this:
- >
- >faxRmApp::faxRmApp() : request("remove") {}
- >
- >I don't understand why the request("remove") initialization is outside
- >the brackets.
- >
- Standard initialization syntax. The initializations between the ":" and
- "{" are performed first. Typically, this syntax is for initializing
- base classes and reference member variables, but works just as well
- for ordinary data members. In fact, if the data member is an object,
- and it's not included in the initializer list, it will be constructed
- with its default constructor and then reinitialized again in the body
- of the derived object's constructor. This could result in significant
- inefficiencies in certain cases. I tend to initialize everything in
- the initializer part, often ending up with an empty constructor body.
-
- >The next questions are even dumber. :)
- >
- >Question #2
- >
- >Why does the scope of the external declaration not extend beyond the
- >inner block?
- >
- >{
- >{
- >extern E;
- >E = 0;
- >}
- >E = 1;
- >}
- >
- Don't confuse scope and duration (lifetime) of an aobject. The
- scope (ability to access) is restricted to the scope within which
- declared. The object (integer variable E in this case) exists
- throughout the lifetime of the entire program, but accessibility
- is limited to the blocks that contain the extern declaration --
- and the block within which it's defined.
- >
- >
- >Question #3
- >
- >Is there any difference between these two declarations?
- >
- >char *s1 = "hello";
- >char s2[] = "hello";
- >
- Yes. You can change s1 to point to some other string later on;
- s2, however, will always be the address of a string, at least
- for its duration.
-
- Another thing, the first form creates a string
- in permament memory containing "hello", and s1 is a pointer variable
- whose initial value is the address of this "hello" string. This
- "hello" string will continue to exist even after s1 is destroyed.
- In the second form, the string is allocated at the place where
- s2 is defined and ceases to exist when s2 is disposed of. Often,
- s2 is stack-allocated and disappears when the function exits.
-
- --
- Pete Grant
- Kalevi, Inc.
- Software Engineering & development
-